home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Toolbox / ROMResourceDump 1.0d1 / ROMResourceDump.p < prev    next >
Encoding:
Text File  |  1996-10-04  |  6.2 KB  |  206 lines  |  [TEXT/CWIE]

  1. program ROMResourceDump;
  2.  
  3. (*
  4.     You may incorporate this sample code into your applications without
  5.     restriction, though the sample code has been provided "AS IS" and the
  6.     responsibility for its operation is 100% yours.  However, what you are
  7.     not permitted to do is to redistribute the source as "DSC Sample Code"
  8.     after having made changes. If you're going to re-distribute the source,
  9.     we require that you make it clear in the source that the code was
  10.     descended from Apple Sample Code, but that you've made changes.
  11. *)
  12.  
  13.     uses
  14.         Types,
  15.         Memory,
  16.         LowMem,
  17.         Files,
  18.         Resources;
  19.  
  20.  
  21.         procedure CopyROMResource(romResourceH : Handle; destRefnum : integer);
  22.             (*    This routine copies a specific ROM resource into the resource file
  23.                     denoted by destRefnum.  Any error conditions get written to
  24.                     the console.
  25.  
  26.                     The main complications are:
  27.                         
  28.                         1.    Have to call LMSetROMMapInsert even for GetResInfo.
  29.                                 
  30.                         2.    Have to copy the resources from out of the ROM before
  31.                                 adding them into the destination resource file.
  32.                                 
  33.                         3.    Have to deal with memory issues, ie we don't want to fill
  34.                                 up our application heap with resources and then run out of
  35.                                 memory.
  36.                     
  37.             *)
  38.             var
  39.                 err : OSStatus;
  40.                 err2 : OSStatus;
  41.  
  42.                 romResourceType : ResType;
  43.                 romResourceID : integer;
  44.                 romResourceName : Str255;
  45.                 romResourceSize : longint;
  46.         begin
  47.             (*    Get lots of information about the resource.  You have to set ROMMapInsert,
  48.                     even for a simple call like GetResInfo.  If you don't do this, things just
  49.                     don't work properly.
  50.             *)
  51.             LMSetROMMapInsert(-1);
  52.             GetResInfo(romResourceH, romResourceID, romResourceType, romResourceName);
  53.             romResourceSize := GetHandleSize(romResourceH);
  54.  
  55.             writeln('    Resource ''', romResourceType, ''' ID=', romResourceID:1, ' size = ', romResourceSize:1, 
  56.                             ' "', romResourceName, '"');
  57.             
  58.             err := HandToHand(romResourceH);
  59.             if err = noErr then begin
  60.                 UseResFile(destRefnum);
  61.                 
  62.                 AddResource(romResourceH, romResourceType, romResourceID, romResourceName);
  63.                 err := ResError;
  64.                 
  65.                 (* Have to call UpdateResFile before calling ReleaseResource.  This allows
  66.                     the Resource Manager to actually dispose of the memory used by the resource
  67.                     because it's already been written to a file.  If you do this the other
  68.                     way, the Resource Manager hangs on to the new resource pending a call
  69.                     to UpdateResFile, but then doesn't remember to release it when it gets
  70.                     one.
  71.                 *)
  72.                 UpdateResFile(destRefnum);
  73.                 err2 := ResError;
  74.                 if err = noErr then begin
  75.                     err := err2;
  76.                 end; (* if *)
  77.                 
  78.                 ReleaseResource(romResourceH);
  79.                 
  80.                 UseResFile(0);
  81.             end; (* if *)
  82.  
  83.             if err <> noErr then begin
  84.                 writeln('    ••• error writing resource ', err:1);
  85.             end; (* if *)
  86.     end; (* CopyROMResource *)
  87.  
  88.  
  89.     function DumpROMResourcesToResourceFile(destRefnum : integer) : OSStatus;
  90.         (* This routine dumps all of the resources in the ROM into the resource file
  91.                 whose reference number is destRefnum.  The basic implementation
  92.                 is a standard:
  93.                 
  94.                     for type = 1 to count of types
  95.                         for index = 1 to count of resources in this type
  96.                             get indexed resource
  97.                                 put it our file
  98.                 
  99.                 This technique normally works for normal resource files but, as long
  100.                 as you set ROMMapInsert before calling the Resource Manager, the
  101.                 technique works equally well for ROM resources.
  102.                 
  103.                 This routine doesn't deal with errors particularly well, especially
  104.                 out of memory errors.
  105.         *)
  106.         var
  107.             saveResFile : integer;
  108.             
  109.             typeCount : integer;
  110.             typeIndex : integer;
  111.             thisType : ResType;
  112.             resourceCount : integer;
  113.             resourceIndex : integer;
  114.             
  115.             foundResourceH : Handle;
  116.     begin
  117.         saveResFile := CurResFile;
  118.         UseResFile(0);
  119.  
  120.         (* Count the number of resource types in the ROM. *)            
  121.         LMSetROMMapInsert(-1);
  122.         typeCount := Count1Types;
  123.         writeln('Number of types = ', typeCount:1);
  124.         
  125.         (* Iterate through each of the types. *)
  126.         for typeIndex := 1 to typeCount do begin
  127.  
  128.             (* Get the information for this type. *)
  129.             LMSetROMMapInsert(-1);
  130.             Get1IndType(thisType, typeIndex);
  131.                         
  132.             (* Count the number of resources of this type in the ROM. *)            
  133.             LMSetROMMapInsert(-1);
  134.             resourceCount := Count1Resources(thisType);
  135.             writeln('  Number of resources for type ''', thisType, ''' = ', resourceCount:1);
  136.  
  137.             (* Iterate through each of the resources. *)
  138.             for resourceIndex := 1 to resourceCount do begin
  139.                 
  140.                 (* Get this resource. *)
  141.                 LMSetROMMapInsert(-1);
  142.                 LMSetTmpResLoad(-1);
  143.                 foundResourceH := Get1IndResource(thisType, resourceIndex);
  144.                 
  145.                 if foundResourceH = nil then begin
  146.                     writeln('   ••• Could not get resource ''', thisType, ''' index ', resourceIndex:1);
  147.                 end else begin
  148.                     (* Copy the resource into the destination resource file. *)
  149.                     CopyROMResource(foundResourceH, destRefnum);
  150.                 end; (* if *)
  151.             end; (* for *)
  152.         end; (* for *)
  153.         
  154.         UseResFile(saveResFile);
  155.         
  156.         DumpROMResourcesToResourceFile := noErr;
  157.     end; (* DumpROMResourcesToResourceFile *)
  158.     
  159.     
  160.     (*    The main line simply opens a unique destination resource file
  161.             and then calls DumpROMResourcesToResourceFile to copy the ROM
  162.             resources into it.
  163.     *)
  164.     
  165.     const
  166.         kOutputFileName = 'ROM Resource Dump File';
  167.         
  168.     var
  169.         err : OSStatus;
  170.         junk : OSStatus;
  171.         fcbPBlock : FCBPBRec;
  172.         retryCount : integer;
  173.         dumpFileSpec : FSSpec;
  174.         dumpFileRefnum : integer;
  175. begin
  176.     writeln('ROMResourceDump');
  177.     
  178.     fcbPBlock.ioRefNum := CurResFile;
  179.     err := PBGetFCBInfoSync(@fcbPBlock);
  180.  
  181.     if err = noErr then begin
  182.         junk := FSMakeFSSpec(fcbPBlock.ioVRefNum, fcbPBlock.ioFCBParID, kOutputFileName, dumpFileSpec);
  183.         retryCount := 0;
  184.         repeat
  185.             FSpCreateResFile(dumpFileSpec, 'RSED', 'rsrc', 0);
  186.             err := ResError;
  187.             if err <> noErr then begin
  188.                 retryCount := retryCount + 1;
  189.                 junk := FSMakeFSSpec(fcbPBlock.ioVRefNum, fcbPBlock.ioFCBParID, stringof(kOutputFileName, ' ', retryCount:1), dumpFileSpec);
  190.             end; (* if *)
  191.         until (err = noErr) or (retryCount > 10);
  192.     end; (* if *)
  193.     
  194.     if err = noErr then begin
  195.         dumpFileRefnum := FSpOpenResFile(dumpFileSpec, fsRdWrPerm);
  196.         err := ResError;
  197.     end; (* if *)
  198.     
  199.     if err = noErr then begin
  200.         err := DumpROMResourcesToResourceFile(dumpFileRefnum);
  201.         CloseResFile(dumpFileRefnum);
  202.     end; (* if *)
  203.     
  204.     writeln('Done. Press command-Q to Quit.');
  205. end. (* ROMResourceDump *)
  206.